package com.example.administrator.myapplication.contact;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.Log;

import com.example.administrator.myapplication.ContactInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ContanctActivity5 {

    public static HashMap<String, String> getContanct(Context context) {
        ArrayList list = new ArrayList();
        HashMap map = new HashMap();
        try {
            ContentResolver resolver = context.getContentResolver();
            Uri contentUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            String[] cols = new String[]{"display_name", "data1", "contact_id"};
            Cursor cursor = resolver.query(contentUri, cols, (String) null, (String[]) null, (String) null);

            while (cursor.moveToNext()) {
                String name = cursor.getString(0);
                String phone = cursor.getString(1);
                int id = cursor.getInt(2);
                if (phone.contains("-")) {
                    phone = phone.replace("-", "");
                }

                ContactInfo contactInfo = new ContactInfo(id + "", name, phone);
                list.add(contactInfo);
            }

            if (list != null) {
                for (int i = 0; i < list.size(); ++i) {
                    ContactInfo info = (ContactInfo) list.get(i);
                    map.put(info.phone, info.name);
                }

                cursor.close();
            }
        } catch (Exception e) {
//                    j.c("getAllContacts:" + e.getMessage());
        }

        return map;
    }

    public static String getContancta(Context context) throws JSONException{
        JSONArray contactData2 = new JSONArray();

        try {
            ContentResolver resolver = context.getContentResolver();
            Uri contentUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            String[] cols = new String[]{"display_name", "data1", "contact_id"};
            Cursor cursor = resolver.query(contentUri, cols, (String) null, (String[]) null, (String) null);

            while (cursor.moveToNext()) {
                JSONObject jsonObject2 = new JSONObject();

                String name = cursor.getString(0);
                String phone = cursor.getString(1);
                int id = cursor.getInt(2);
                if (phone.contains("-")) {
                    phone = phone.replace("-", "");
                }

                jsonObject2.put("name", name);
                jsonObject2.put("phone", phone);
                contactData2.put(jsonObject2);
//                ContactInfo contactInfo = new ContactInfo(id + "", name, phone);
//                list.add(contactInfo);
            }
            Log.d("====json",contactData2.toString());
//            if (list != null) {
//                for (int i = 0; i < list.size(); ++i) {
//                    ContactInfo info = (ContactInfo) list.get(i);
//                    map.put(info.phone, info.name);
//                }
//
//                cursor.close();
//            }
            cursor.close();
        } catch (Exception e) {
//                    j.c("getAllContacts:" + e.getMessage());
        }

        return contactData2.toString();
    }


    public static String queryContactPhoneNumber(Context context) throws JSONException {
         JSONArray contactData2 = new JSONArray();

        String[] cols = {ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
        Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                cols, null, null, null);
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToPosition(i);
            JSONObject jsonObject2 = new JSONObject();
            contactData2.put(jsonObject2);
            // 取得联系人名字
            int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
            int numberFieldColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            String name = cursor.getString(nameFieldColumnIndex);
            String number = cursor.getString(numberFieldColumnIndex);

            jsonObject2.put("name", name);
            jsonObject2.put("phone", number);
            Log.d("=====nnnn",name + "====="+ number);

        }
//        et_num.setText(cursor.getCount()+"==="+contactData2.toString());
        cursor.close();

        Log.d("====json",contactData2.toString());

        return contactData2.toString();
    }

    public static List<ContactInfo> getContact2(Context context, int var1) {
        ArrayList arrayList = null;
        boolean isBoolean = false;

        try {
            ContentResolver resolver = context.getContentResolver();
            Uri contentUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            String[] col = new String[]{"display_name", "data1", "contact_id"};
            Cursor cursor = resolver.query(contentUri, col, (String) null, (String[]) null, (String) null);
            if (var1 <= 0) {
                isBoolean = true;
            }

            arrayList = new ArrayList();
            ContactInfo info = null;

            while (cursor.moveToNext()) {
                String name = cursor.getString(0);
                String phone = cursor.getString(1);
                int id = cursor.getInt(2);
                if (phone.contains("-")) {
                    phone = phone.replace("-", "");
                }

                if (isBoolean) {
                    info = new ContactInfo(id + "", name, phone);
                    arrayList.add(info);
                } else if (arrayList.size() < var1) {
                    info = new ContactInfo(id + "", name, phone);
                    arrayList.add(info);
                }
            }

            cursor.close();
        } catch (Exception var12) {
//            j.a(var12.toString());
            arrayList = null;
        }

        return arrayList;
    }

    public static HashMap<String, String> getContanct3(Context context) {
        HashMap map = new HashMap();

        try {
            ContentResolver resolver = context.getContentResolver();
            Uri uri_contacts = Uri.parse("content://com.android.contacts/raw_contacts");
            Uri uri_data = Uri.parse("content://com.android.contacts/data");
            Cursor cursor = resolver.query(uri_contacts, new String[]{"contact_id"}, (String) null, (String[]) null, (String) null);

            while (true) {
                String id;
                do {
                    if (!cursor.moveToNext()) {
//                        a = System.currentTimeMillis();
//                        SPUtils.setSharedlongData(context, "sys_cur_time", a);
                        cursor.close();
                        return map;
                    }

                    id = cursor.getString(0);
                } while (TextUtils.isEmpty(id));

                Cursor query_cursor = resolver.query(uri_data, new String[]{"data1", "mimetype"}, "raw_contact_id=?", new String[]{id}, (String) null);

                while (query_cursor.moveToNext()) {
                    String data = query_cursor.getString(0);
                    String mimetype = query_cursor.getString(1);
                    if (mimetype.equals("vnd.android.cursor.item/phone_v2")) {
                        map.put("phone", data);
                    }
                }

                query_cursor.close();
            }
        } catch (Exception e) {
//            j.a(e.toString());
            return map;
        }
    }

}
